<template>
  <div class="app-container">
    <!-- 靠右的按钮 -->
    <page-tools>
      <template v-slot:after>
        <el-button type="primary" size="small" @click="AddPermission('0',1)">添加权限</el-button>
      </template>
    </page-tools>
    <!-- 表格 -->
    <el-table border :data="list" row-key="id" default-expand-all>
      <el-table-column label="序号" type="index" width="80" />

      <el-table-column label="名称" prop="name" />
      <el-table-column label="标识" prop="code" />
      <el-table-column label="描述" prop="description" />
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-button v-if="row.type === 1" type="text" icon="el-icon-circle-plus-outline" @click="AddPermission(row.id,2)">添加</el-button>
          <el-button type="text" icon="el-icon-edit" @click="editFn(row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" style="color: #f56c6c" @click="delFn(row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <AddPermission ref="AddPermission" :dialog-visible.sync="dialogVisible" />
  </div>
</template>

<script>
import { getPermissionList, delPermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import AddPermission from './components/add-permission.vue'
export default {
  name: 'PermissionIndex',
  components: {
    AddPermission
  },
  data() {
    return {
      list: [],
      dialogVisible: false
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async  getPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res, '0')
    },
    AddPermission(pid, type) {
      this.dialogVisible = true
      this.$refs.AddPermission.pid = pid
      this.$refs.AddPermission.pid = type
    },
    editFn(row) {
      this.$refs.AddPermission.formData = { ...row }
      this.dialogVisible = true
    },
    async delFn(id) {
      this.$confirm('确认删除吗', '提示', {
        type: 'warning'
      })
      try {
        await delPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    }

  }
}
</script>

<style lang="scss" scoped>

</style>

